package com.isunland.managesystem.ui;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.NavUtils;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.ListView;
import com.google.gson.Gson;
import com.isunland.managesystem.R;
import com.isunland.managesystem.adapter.CheckOutListAdapter;
import com.isunland.managesystem.base.BaseListFragment;
import com.isunland.managesystem.common.ApiConst;
import com.isunland.managesystem.entity.CheckOutListContent;
import com.isunland.managesystem.entity.CheckOutListOriginal;
import com.isunland.managesystem.entity.SuccessMessage;
import com.isunland.managesystem.utils.ToastUtil;
import java.util.ArrayList;
import java.util.HashMap;

/**
 * 对象功能: 外出子表列表
 * 开发公司: 北京信普达系统工程有限公司
 * 开发人员: HuXiaoDan
 * 创建时间: 2017/3/3 11:00
 */
public class CheckOutListFragment extends BaseListFragment {
  protected static final String EXTRA_PID =
      "com.isunland.managesystem.ui.CheckOutListFragment.EXTRA_PID";
  protected static final String EXTRA_DATASTATUS =
      "com.isunland.managesystem.ui.CheckOutListFragment.EXTRA_DATASTATUS";
  protected static final String EXTRA_IFLATERWRITE =
      "com.isunland.managesystem.ui.CheckOutListFragment.EXTRA_IFLATERWRITE";
  protected static final String EXTRA_FROM =
      "com.isunland.managesystem.ui.CheckOutListFragment.EXTRA_FROM";
  private static final int REQUEST_DETAIL = 0;
  private static String mpid = "-1";
  private static String mdatastatus;
  protected static final String TODO = "todo";
  private CheckOutListAdapter mAdapter;
  private static String mIfLaterWriter;
  private String mFrom;

  @Override public void onViewCreated(View view, Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);
    mListview.setDividerHeight(0);
  }

  @Override public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setBack(true);
    mpid = getArguments().getString(EXTRA_PID);
    mdatastatus = getArguments().getString(EXTRA_DATASTATUS);
    mIfLaterWriter = getArguments().getString(EXTRA_IFLATERWRITE);
    mFrom = getArguments().getString(EXTRA_FROM);
    if (mFrom != null && TODO.equalsIgnoreCase(mFrom)) {
      setTitleCustom(R.string.checkDetail);
    }
  }

  @Override public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    if (mdatastatus.equalsIgnoreCase("new") || mdatastatus.equalsIgnoreCase("abort")) {
      inflater.inflate(R.menu.menu_add, menu);
    }
  }

  @Override public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
      case android.R.id.home:
        if (NavUtils.getParentActivityName(getActivity()) != null) {
          NavUtils.navigateUpFromSameTask(getActivity());
        }

        break;
      case R.id.menu_item_add:
        if ("-1".equals(mpid)) {
          ToastUtil.show(R.string.save_main);
        } else {
          //外出单
          Intent i = new Intent(getActivity(), CheckOutActivity.class);
          i.putExtra(CheckOutNewFragment.EXTRA_PID, mpid);
          i.putExtra(CheckOutNewFragment.EXTRA_IFLATERWRITER, mIfLaterWriter);
          startActivityForResult(i, REQUEST_DETAIL);
        }
        break;
      default:
        break;
    }
    return super.onOptionsItemSelected(item);
  }

  @Override public String getUrl() {
    return ApiConst.URL_OUT_LIST;
  }

  @Override public HashMap<String, String> getparameters() {
    HashMap<String, String> params = new HashMap<>();
    params.put("pid", mpid);
    params.put("ifLaterWrite", "");//查出所有申请和补填单据
    return params;
  }

  @Override public void resolveResponse(String json) {

    CheckOutListOriginal original = new Gson().fromJson(json, CheckOutListOriginal.class);

    if (SuccessMessage.SUCCESS.equalsIgnoreCase(original.getResult())
        && original.getData() != null) {
      //获取列表成功
      ArrayList<CheckOutListContent> rows = (ArrayList<CheckOutListContent>) original.getData();

      setAdapter(rows);
    } else {
      ToastUtil.show("获取列表失败");
    }
  }

  private void setAdapter(ArrayList<CheckOutListContent> rows) {
    if (rows == null) {
      rows = new ArrayList<>();
    }
    if (mAdapter == null) {
      mAdapter = new CheckOutListAdapter(getActivity(), rows);
      setListAdapter(mAdapter);
      return;
    }
    mAdapter.clear();
    mAdapter.addAll(rows);
    ((CheckOutListAdapter) getListAdapter()).notifyDataSetChanged();
  }

  @Override public void onListItemClick(ListView l, View v, int position, long id) {
    CheckOutListContent content = mAdapter.getItem(position - 1);
    Intent i = new Intent(getActivity(), CheckOutActivity.class);
    i.putExtra(CheckOutNewFragment.EXTRA_CONTENT, content);
    i.putExtra(CheckOutNewFragment.EXTRA_PID, mpid);
    i.putExtra(CheckOutNewFragment.EXTRA_FROM, mFrom);
    if (mFrom != null && mFrom.equalsIgnoreCase(TODO)) {
      //流程实际时间确认按补填处理
      i.putExtra(CheckOutNewFragment.EXTRA_IFLATERWRITER, mIfLaterWriter);
    } else {
      i.putExtra(CheckOutNewFragment.EXTRA_IFLATERWRITER, content.getIfLaterWrite());
    }
    startActivityForResult(i, REQUEST_DETAIL);
  }

  @Override public void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (resultCode != Activity.RESULT_OK) {
      return;
    }
    volleyPost();
  }

  protected static void updataId(String pid, String ifLaterWriter) {
    mpid = pid;
    mIfLaterWriter = ifLaterWriter;
  }

  public static CheckOutListFragment newInstance(String pid, String dataStatus,
      String ifLaterWrite) {
    Bundle args = new Bundle();
    args.putString(EXTRA_PID, pid);
    args.putString(EXTRA_DATASTATUS, dataStatus);
    args.putString(EXTRA_IFLATERWRITE, ifLaterWrite);
    CheckOutListFragment fragment = new CheckOutListFragment();
    fragment.setArguments(args);
    return fragment;
  }

  public static CheckOutListFragment newInstance(String pid, String dataStatus, String ifLaterWrite,
      String from) {
    Bundle args = new Bundle();
    args.putString(EXTRA_PID, pid);
    args.putString(EXTRA_DATASTATUS, dataStatus);
    args.putString(EXTRA_IFLATERWRITE, ifLaterWrite);
    args.putString(EXTRA_FROM, from);
    CheckOutListFragment fragment = new CheckOutListFragment();
    fragment.setArguments(args);
    return fragment;
  }
}
